﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sweepids.Models.Dao;
using Sweepids.Models.Object;

namespace Sweepids.Controllers
{
    public class SettingsController : Controller
    {
        //
        // GET: /Settings/

        public ActionResult Index()
        {

            try
            {
                var user = (User)Session["User"];
                user.Fill();
                return View(user);
            }
            catch (Exception)
            {
                TempData["Error"] = "An error occured while viewing the settings.";
                return RedirectToAction("Index", "Home");
            }
        }
        [HttpPost]
        public ActionResult EditInfo(string username, string password, string firstname, string lastname, string email, string address, string city, string state, string zip, string cardNumber, string expDate, string secCode )
        {
            try
            {
                var zipDao = new ZipDao();
                var creditDao = new CreditCardDao();
                var userDao = new UserDao();

                var zipCode = new Zip { City = city, State = state, ZipId = zip };
                var creditCardObj = new CreditCard
                {
                    CardNum = cardNumber,
                    ExpDate = DateTime.Parse(expDate),
                    SecCode = secCode
                };
                var user = new User
                {
                    Username = username,
                    Password = password,
                    FirstName = firstname,
                    LastName = lastname,
                    Email = email,
                    Address = address,
                    ZipObj = zipCode,
                    Zip = zip,
                    CreditCardObj = creditCardObj
                };

                zipDao.Update(zipCode);
                creditDao.Update(creditCardObj);
                userDao.Update(user);

                Session["User"] = user;

                TempData["Success"] = "Successfully updated the user.";
                return View("Index", user);
            }
            catch (Exception)
            {
                TempData["Error"] = "An error occured while updating the user.";
                return RedirectToAction("Index", "Home");
            }
        }

    }
}
